package com.lineage.data.item_etcitem.teleport;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.lock.CharBookReading;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Location;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.model.map.L1WorldMap;
import com.lineage.server.model.skill.L1BuffUtil;
import com.lineage.server.serverpackets.S_Message_YN;
import com.lineage.server.serverpackets.S_Paralysis;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1BookMark;
import com.lineage.server.world.World;
import java.util.ArrayList;
import java.util.Iterator;

public class Scroll_Mass_Teleport extends ItemExecutor {
  public static ItemExecutor get() {
    return new Scroll_Mass_Teleport();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    if (pc.hasSkillEffect(4017)) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(1413));
      return;
    } 
    int mapID = data[0];
    int btele = data[1];
    boolean isTeleport = L1WorldMap.get().getMap((short)mapID).isEscapable();
    if (!isTeleport) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(647));
      pc.sendPackets((ServerBasePacket)new S_Paralysis(7, false));
    } else {
      L1BookMark bookm = CharBookReading.get().getBookMark(pc, btele);
      if (bookm != null) {
        pc.getInventory().removeItem(item, 1L);
        ArrayList<L1Object> objList = World.get().getVisiblePoint(pc.getLocation(), 3, pc.get_showId());
        Iterator<L1Object> iterator = objList.iterator();
        while (iterator.hasNext()) {
          L1Object tgObj = iterator.next();
          if (tgObj instanceof L1PcInstance) {
            L1PcInstance tgPc = (L1PcInstance)tgObj;
            if (tgPc.isDead() || tgPc.getClanid() == 0 || tgPc.getClanid() != pc.getClanid() || 
              tgPc.isPrivateShop())
              continue; 
            L1BuffUtil.cancelAbsoluteBarrier(tgPc);
            tgPc.setTeleportX(bookm.getLocX());
            tgPc.setTeleportY(bookm.getLocY());
            tgPc.setTeleportMapId(bookm.getMapId());
            tgPc.sendPackets((ServerBasePacket)new S_Message_YN(748));
          } 
        } 
        L1Teleport.teleport(pc, bookm.getLocX(), bookm.getLocY(), bookm.getMapId(), 5, true);
      } else {
        pc.getInventory().removeItem(item, 1L);
        L1Location newLocation = pc.getLocation().randomLocation(200, true);
        int newX = newLocation.getX();
        int newY = newLocation.getY();
        short newMapId = (short)newLocation.getMapId();
        ArrayList<L1Object> objList2 = World.get().getVisiblePoint(pc.getLocation(), 3, pc.get_showId());
        Iterator<L1Object> iterator2 = objList2.iterator();
        while (iterator2.hasNext()) {
          L1Object tgObj2 = iterator2.next();
          if (tgObj2 instanceof L1PcInstance) {
            L1PcInstance tgPc2 = (L1PcInstance)tgObj2;
            if (tgPc2.isDead() || tgPc2.getClanid() == 0 || tgPc2.getClanid() != pc.getClanid() || 
              tgPc2.isPrivateShop())
              continue; 
            L1BuffUtil.cancelAbsoluteBarrier(tgPc2);
            tgPc2.setTeleportX(newX);
            tgPc2.setTeleportY(newY);
            tgPc2.setTeleportMapId(newMapId);
            pc.sendPackets((ServerBasePacket)new S_Message_YN(748));
          } 
        } 
        L1Teleport.teleport(pc, newX, newY, newMapId, 5, true);
      } 
      L1BuffUtil.cancelAbsoluteBarrier(pc);
    } 
  }
}
